package com.friend.chat.page.view.friend

import android.app.Activity
import android.content.Intent
import android.view.View
import android.widget.CheckBox
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.friend.chat.R
import com.friend.chat.base.BaseActivity
import com.friend.chat.databinding.ActivityAddGroupBinding
import com.friend.chat.page.bean.FriendBean
import com.friend.chat.page.bean.UserBean
import com.friend.chat.page.vm.AddGroupViewModel
import com.friend.chat.page.widget.MyInputDialog
import com.friend.chat.utils.GlideUtils
import com.friend.chat.utils.SPUtils
import com.star.libwidget.star_dialog.common.StarDialog
import com.star.libwidget.star_dialog.input.StarInputDialog
import com.star.libwidget.star_dialog.input.StarInputDialogClickListener

class AddGroupActivity : BaseActivity<ActivityAddGroupBinding, AddGroupViewModel>() {
    private lateinit var mContactListAddAdapter: ContactListAddAdapter
    private lateinit var mUserBean: UserBean

    companion object {
        fun open(activity: Activity) {
            val intent = Intent(activity, AddGroupActivity::class.java)
            activity.startActivity(intent)
        }
    }

    override fun yourViewModel(): AddGroupViewModel {
        return AddGroupViewModel()
    }

    override fun yourViewBing(): ActivityAddGroupBinding {
        return ActivityAddGroupBinding.inflate(layoutInflater)
    }

    override fun init() {
        mViewBinding.commonBar.barTitle.text = "添加群成员"
        mViewBinding.commonBar.barTitle.visibility = View.VISIBLE
        mViewBinding.commonBar.barBack.setOnClickListener { finish() }

        mUserBean = SPUtils.getInstance().getObject("user") as UserBean

        val linearLayoutManager = object : LinearLayoutManager(this) {
            override fun canScrollHorizontally(): Boolean {
                return false
            }

            override fun canScrollVertically(): Boolean {
                return true
            }
        }
        mContactListAddAdapter = ContactListAddAdapter()
        mViewBinding.rvChatListAdd.layoutManager = linearLayoutManager
        mViewBinding.rvChatListAdd.adapter = mContactListAddAdapter
        mVModel.getContactList(mUserBean.uid)
    }

    override fun setClick() {
        super.setClick()
        mViewBinding.btnAddComplete.setOnClickListener {
            val dataList = mContactListAddAdapter.data as ArrayList<FriendBean>
            val memberList = ArrayList<String>()
            for (data in dataList) {
                if (data.checked) {
                    memberList.add(data.friendId)
                }
            }
            if (memberList.size<2){
                mVModel.toast("请选择群成员,至少两个")
            }else{
                mVModel.toast("选择成功")
                MyInputDialog(this)
                     .setTitle("请输入群名称")
                     .singleButton(true)
                     .setOnStarInputDialogClickListener(object :StarInputDialogClickListener{
                     override fun onCancel() {

                     }

                     override fun onConfirm(data: String) {
                         mVModel.createGroup(mUserBean.uid,data,memberList)

                     }

                 }).show()

               // submit()
            }
        }
    }

    override fun onNetListener() {
        super.onNetListener()
        mVModel.contactListResponse.observe(this) {
            val data = it.data
            mContactListAddAdapter.setList(data)
        }
        mVModel.addFriendResponse.observe(this){
            mVModel.toast("添加成功")
            finish()
        }
    }

    class ContactListAddAdapter :
        BaseQuickAdapter<FriendBean, BaseViewHolder>(R.layout.item_contact_list_add) {
        override fun convert(holder: BaseViewHolder, item: FriendBean) {
            holder.setIsRecyclable(false)
            val headerView = holder.getView<ImageView>(R.id.iv_item_contact_header)
            val nameView = holder.getView<TextView>(R.id.tv_item_contact_name)
            val checkBox = holder.getView<CheckBox>(R.id.cb_item_contact)

            GlideUtils.loadRadiusImg1(headerView, item.friendHeader, 4)
            nameView.text = item.friendNickName
            checkBox.isChecked = item.checked
            checkBox.setOnCheckedChangeListener { compoundButton, isChecked ->
                item.checked = isChecked
            }
            //  addChildClickViewIds(R.id.rl_item_contact)
        }

    }
}